﻿/**
 * 时间日期格式化
 */

const formatTime = date => {
  var value = new Date(date * 1000);
  const year = value.getFullYear()
  const month = value.getMonth() + 1
  const day = value.getDate()
  const hour = value.getHours()
  const minute = value.getMinutes()
  const second = value.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function formatTimeTillMin(number, format) {

  var formateArr = ['Y', 'M', 'D', 'h', 'm'];
  var returnArr = [];

  var date = new Date(number);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNum(date.getMonth() + 1));
  returnArr.push(formatNum(date.getDate()));

  returnArr.push(formatNum(date.getHours()));
  returnArr.push(formatNum(date.getMinutes()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

//数据转化  
function formatNum(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

module.exports = {
  formatTime: formatTime,
  formatTimeTillMin:formatTimeTillMin,
  formatNum: formatNum
}
